package com.yupi.springbootinit.config;

import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ClassName: RedissonConfig
 * Package: com.yupi.springbootinit.config
 * Description: Redisson的配置
 *
 * @Author:
 * @Create: 2024/4/2 - 8:42
 * @Version: v1.0
 */
@Configuration
@ConfigurationProperties(prefix = "spring.redis")
@Data
public class RedissonConfig {

    private String host;

    private String port;

    private String password;

    private int redissonDatabase;

    @Bean
    public RedissonClient redissonClient() {
        // 1. Create config object
        Config config = new Config();
        String redisAddress = String.format("redis://%s:%s", host, port);
        //单机redis的连接方式
        config.useSingleServer()
                .setAddress(redisAddress)
                .setPassword(password)
                .setDatabase(redissonDatabase);


        /*
        这里是集群的配置
        config.useClusterServers()
                // use "rediss://" for SSL connection
                .addNodeAddress("redis://127.0.0.1:7181");*/
        RedissonClient redisson = Redisson.create(config);
        return redisson;
    }
}
